package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.enums.TransTypeEnum;
import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.TransFlowMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.UserAccountService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.atguigu.srb.mq.service.MqService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户账户 服务实现类
 * </p>
 *
 * @author lsyandwh
 * @since 2022-12-02
 */
@Transactional
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private TransFlowMapper transFlowMapper;

    @Autowired
    private MqService mqService;

    @Override
    public String commitCharge(BigDecimal chargeAmt, String userId) {

        //1.判断用户账户绑定状态
        UserInfo userInfo = userInfoMapper.selectById(Long.parseLong(userId));
        Integer bindStatus = userInfo.getBindStatus();
        Assert.equals(bindStatus, UserBindEnum.BIND_OK.getStatus(), ResponseEnum.USER_NO_BIND_ERROR);

        //2.调用汇付宝接口充值，生成充值表单
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentBillNo", LendNoUtils.getNo());//充值单号
        paramMap.put("bindCode",userInfo.getBindCode());
        paramMap.put("chargeAmt", chargeAmt);//充值金额
        paramMap.put("feeAmt",new BigDecimal("0"));//商户手续费
        paramMap.put("returnUrl",HfbConst.RECHARGE_RETURN_URL);// 汇付宝页面点击回跳尚融宝路径
        paramMap.put("notifyUrl",HfbConst.RECHARGE_NOTIFY_URL);// 汇付宝内部回调尚融宝接口地址
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        //发送请求
        String form = FormHelper.buildForm(HfbConst.RECHARGE_URL, paramMap);

        return form;
    }

    @Override
    public String userAccountNotify(Map<String, Object> parameterMap) {

        //1.根据bind_code获取用户信息
        String bindCode = (String) parameterMap.get("bindCode");
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bind_code",bindCode);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        //2.根据用户信息修改账户余额
        String chargeAmt = (String) parameterMap.get("chargeAmt");

        //获取账户信息
        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userInfo.getId());
        UserAccount userAccount = baseMapper.selectOne(queryWrapper);

        //计算用户余额
        BigDecimal amount = userAccount.getAmount();
        BigDecimal add = amount.add(new BigDecimal(chargeAmt));

        userAccount.setAmount(add);
        //更新账户余额
        baseMapper.updateById(userAccount);

        //3.生成订单流水
        TransFlow transFlow = new TransFlow();
        transFlow.setUserId(userInfo.getId());
        transFlow.setUserName(userInfo.getName());
        transFlow.setTransType(TransTypeEnum.RECHARGE.getTransType());//设置交易类型
        transFlow.setTransTypeName(TransTypeEnum.RECHARGE.getTransTypeName());//交易类型名称
        transFlow.setTransAmount(new BigDecimal(chargeAmt));//交易金额
        transFlow.setTransNo((String) parameterMap.get("agentBillNo"));//交易单号

        transFlowMapper.insert(transFlow);

        //向RabbitMQ中发送消息
        mqService.sendMessage("rechargeExchange","key","nb666");

        return "success";
    }

    @Override
    public BigDecimal getAccount(String userId) {

        QueryWrapper<UserAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);

        UserAccount userAccount = baseMapper.selectOne(wrapper);
        BigDecimal amount = userAccount.getAmount();

        return amount;
    }
}
